package com.g2.rpc.rpc;

import lombok.Data;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 描述:
 * ${DESCRIPTION}
 *
 * @author lingjiu.kong
 * @version 1.0
 * @date 2021-03-04 0:42
 */
@Data
public class RpcInvocation {

    private static final long serialVersionUID = -4355285085441097045L;
    private static final AtomicLong SEQ = new AtomicLong();
    private long id;
    private String serviceName;
    private String methodName;
    private Class<?>[] parameterTypes;
    private Object[] arguments;

    public RpcInvocation() {
        // 初始化一个ID
        this.setId(incrementAndGet());
    }

    private static long incrementAndGet() {
        long current;
        long next;
        do {
            current = SEQ.get();
            next = current >= 2147483647 ? 0 : current + 1;
        } while (!SEQ.compareAndSet(current, next));

        return current;

    }
}
